﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.IO;
namespace CreaMotion.UserControls
{
	/// <summary>
	/// Interaction logic for ConnectorItem.xaml
	/// </summary>
	public partial class ConnectorItem : UserControl
    {

        #region Parametres
        public developer Developer = new developer();
        public information Information = new information();
        public Dictionary<String, String> Settings = new Dictionary<string, string>();
        #endregion
        
        #region New() +1 Overload

       public ConnectorItem()
		{
			this.InitializeComponent();
		}

        public ConnectorItem(String CfgPath)
        {
            this.InitializeComponent();
            LoadSettings(CfgPath);
        }

        #endregion

        #region Developer

        public class developer
        {
            public String Company = string.Empty;
            public String Version = string.Empty;
            public String Description = string.Empty;
        }

        #endregion

        #region Information

        public class information
        {
            public String Name = string.Empty;
            public String Description = string.Empty;
            public String LogoPath = string.Empty;
            public String ConnectorName = string.Empty;
        }
        #endregion

        public void LoadSettings(String CfgPath)
        {
            if (string.IsNullOrEmpty(CfgPath)) return;
           
            XDocument ConfigContent;
            XElement Temp;

            try
            {
                ConfigContent = XDocument.Load(CfgPath);
                Temp = ConfigContent.Root.Element("Developer");

                //Developer
                Developer.Company = Temp.Attribute("Company").Value;
                Developer.Version = Temp.Attribute("Version").Value;
                Developer.Description = Temp.Attribute("Description").Value;

                Temp = ConfigContent.Root.Element("Information");

                //Information
                Information.Name = Temp.Attribute("Name").Value;
                Information.Description = Temp.Attribute("Description").Value;
                Information.LogoPath = Temp.Attribute("LogoPath").Value;
                Information.ConnectorName = Temp.Attribute("ConnectorName").Value;

                Temp = ConfigContent.Root.Element("Settings");

                foreach (var item in Temp.Elements("Setting"))
                {
                    Settings.Add(item.Attribute("Key").Value, item.Attribute("Value").Value);
                }

                labConnectorName.Text = Information.Name;
                labConnectorDescription.Text = Information.Description;
                ImgLogo.Source = new BitmapImage(new Uri(Path.Combine(Directory.GetCurrentDirectory(),"Config File","Logos",Information.LogoPath)));
            }
            catch
            {
                throw new Exception("Check cfg file");
            }
            finally
            {
                ConfigContent = null;
                Temp = null;
            }
        }
	}
}